package dao

import (
	"server/basic/config"
	"server/basic/model"
)

func HospitalListM() (hospital []model.Hospitals, err error) {
	err = config.DB.Limit(5).Find(&hospital).Error
	if err != nil {
		return []model.Hospitals{}, err
	}
	return hospital, nil
}

func HospitalList(page int32, size int32) (hospital []model.Hospitals, err error) {
	page = page
	if page <= 0 {
		page = 1
	}

	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	err = config.DB.Offset(int(offset)).Limit(int(pageSize)).Find(&hospital).Error
	if err != nil {
		return nil, err
	}
	return hospital, nil
}

func DepartmentList(id int32) (department []model.Departments, err error) {
	err = config.DB.Where("hospital_id=?", id).Find(&department).Error
	if err != nil {
		return nil, err
	}
	return department, nil
}

func DepartmrntLisrWhere(name string, id int32) (department []model.Departments, err error) {
	err = config.DB.Where("hospital_id=?", id).Where("name LIKE ?", name).Find(&department).Error
	if err != nil {
		return nil, err
	}
	return department, nil
}

func RegisterList() (register []model.Registers, err error) {
	err = config.DB.Find(&register).Error
	if err != nil {
		return nil, err
	}
	return register, nil
}

func ThePatientList(id int32) (the []model.ThePatients, err error) {
	err = config.DB.Where("user_id=?", id).Find(&the).Error

	if err != nil {
		return nil, err
	}
	return the, nil
}

func DoctorList() (doctor []model.Doctors, err error) {
	err = config.DB.Find(&doctor).Error

	if err != nil {
		return nil, err
	}
	return doctor, nil
}

func DoctorListtype(doctorType int32) (doctor []model.Doctors, err error) {
	err = config.DB.Where("doctor_type=?", doctorType).Find(&doctor).Error

	if err != nil {
		return nil, err
	}
	return doctor, nil
}
